import time

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_my_ticket_locator import AdminMyTicketLocator
from Case.python_ui.PageLocate.admin_team_ticket_loactor import AdminTeamTicketLocator
from Case.python_ui.PageLocate.admin_ticket_management_locator import AdminTicketManagementLocator


class AdminMyTicketPage(Basepage):
    """
        20240417
    """
    # 退出admin端
    def log_out_admin(self):
        doc = '退出admin端'
        self.wait_elevisble(AdminTicketManagementLocator.out_image,doc)
        self.move_mouse_to_element(AdminTicketManagementLocator.out_image)
        time.sleep(2)
        self.wait_elevisble(AdminTicketManagementLocator.click_log_out,doc)
        self.move_mouse_to_click_element(AdminTicketManagementLocator.click_log_out,doc)
        time.sleep(5)

    # 打开新窗口并切换到新窗口
    def switch_to_new_window(self):
        # 打开一个新窗口
        time.sleep(2)
        self.driver.execute_script("window.open('about:blank', 'new_tab')")
        time.sleep(2)
        # 切换到新窗口
        self.driver.switch_to.window("new_tab")


    """
        
    """
    # 进入team ticket菜单页面
    def enter_my_ticket_page(self):
        time.sleep(2)
        doc = "进入my ticket菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminTicketManagementLocator.dashboard_tab, doc)
                self.click_element(AdminTicketManagementLocator.dashboard_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminTicketManagementLocator.bill_tab, doc)
                self.click_element(AdminTicketManagementLocator.bill_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminMyTicketLocator.my_ticket_tab,doc)
                self.click_element(AdminMyTicketLocator.my_ticket_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    # 进入工单详情页
    def enter_ticket_detail_page(self):
        doc = "My Ticket进入工单详情页"
        self.wait_elevisble(AdminMyTicketLocator.my_ticket_tab, doc)
        self.click_element(AdminMyTicketLocator.my_ticket_tab, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyTicketLocator.ticket_id, doc)
        ticket_no_1 = self.get_element(AdminMyTicketLocator.ticket_id, doc).text
        self.click_element(AdminMyTicketLocator.ticket_id, doc)
        return ticket_no_1

    # 指定ticket id，进入工单详情页
    def enter_ticket_detail_page_use_id(self,ticket_id):
        doc = "My Ticket进入指定的工单详情页"
        self.wait_elevisble(AdminMyTicketLocator.my_ticket_tab, doc)
        self.click_element(AdminMyTicketLocator.my_ticket_tab, doc)
        time.sleep(2)
        self.search_ticket(ticket_id)
        self.wait_elevisble(AdminMyTicketLocator.ticket_id, doc)
        ticket_no_1 = self.get_element(AdminMyTicketLocator.ticket_id, doc).text
        self.click_element(AdminMyTicketLocator.ticket_id, doc)
        return ticket_no_1

    #发送工单消息
    def send_ticket_comment(self):
        doc = "Admin 发送工单消息"
        comments = "This is the content from automated testing"
        self.wait_elevisble(AdminMyTicketLocator.ticket_comments,doc)
        self.send_keys(AdminMyTicketLocator.ticket_comments,comments,doc)
        self.wait_elevisble(AdminMyTicketLocator.ticket_send,doc)
        self.click_element(AdminMyTicketLocator.ticket_send,doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyTicketLocator.ticket_last_comment,doc)
        lats_ticket_comment = self.get_element(AdminMyTicketLocator.ticket_last_comment,doc).text
        assert comments == lats_ticket_comment

    def solve_ticket(self,ticket_id):
        doc = "Admin solve工单"
        time.sleep(5)
        self.wait_elevisble(AdminMyTicketLocator.solve_button,doc)
        self.click_element(AdminMyTicketLocator.solve_button,doc)
        self.wait_elevisble(AdminMyTicketLocator.solve_button_confirm,doc)
        self.click_element(AdminMyTicketLocator.solve_button_confirm,doc)


    def search_ticket(self,ticket_id):
        doc = "Admin 搜索工单"
        time.sleep(2)
        self.wait_elevisble(AdminMyTicketLocator.ticket_status_filter, doc)
        self.click_element(AdminMyTicketLocator.ticket_status_filter, doc)
        self.wait_elevisble(AdminMyTicketLocator.reset, doc)
        self.click_element(AdminMyTicketLocator.reset, doc)
        self.wait_elevisble(AdminMyTicketLocator.ok, doc)
        self.click_element(AdminMyTicketLocator.ok, doc)
        self.wait_elevisble(AdminMyTicketLocator.search_box, doc)
        self.send_keys(AdminMyTicketLocator.search_box, ticket_id, doc)
        self.click_element(AdminMyTicketLocator.search_button, doc)
        time.sleep(5)
        self.wait_elevisble(AdminMyTicketLocator.search_ticket_status, doc)
        status = self.get_element(AdminMyTicketLocator.search_ticket_status, doc).text
        return status

    def admin_check_new_assign_ticket_message(self,ticket_id):
        doc = "管理员检查新分配的工单提醒"
        self.wait_elevisble(AdminMyTicketLocator.my_ticket_tab, doc)
        self.click_element(AdminMyTicketLocator.my_ticket_tab, doc)
        self.search_ticket(ticket_id)
        self.wait_elevisble(AdminTeamTicketLocator.tickets_button2, doc)
        self.click_element(AdminTeamTicketLocator.tickets_button2, doc)
        self.wait_elevisble(AdminMyTicketLocator.my_ticket_new,doc)
        self.wait_elevisble(AdminMyTicketLocator.my_ticket_menu_new,doc)
        new = self.get_element(AdminMyTicketLocator.my_ticket_menu_new,doc).text
        assert new == 'NEW'
        self.wait_elevisble(AdminMyTicketLocator.red_dot,doc)
        red_dot = self.get_element(AdminMyTicketLocator.red_dot,doc).get_attribute("class")
        assert red_dot == "antd4-badge-status-dot"